class MessageManager {
    constructor() {
        this._handlers = {};
    }

    subscribe(type, callback) {
        const handler = {
            callback,
            unsubscribe: () => this.unsubscribe(type, handler)
        };

        this._handlers[type] = this._handlers[type] || [];
        this._handlers[type].push(handler);
    }

    unsubscribe(type, handler) {
        const handlers = this._handlers || [];
        for (let i = 0; i < handlers.length; i++) {
            if (handlers[i] === handler) {
                handlers.splice(i, 1);
                break;
            }
        }
    }

    notify(message) {
        const handlers = this._handlers[message.type] || [];
        for (const handler of handlers) {
            handler.callback(message);
        }
    }
}